package com.djl.mvc.interceptor;

import com.djl.crowd.entity.Admin;
import com.djl.crowd.util.CrowdConstant;
import com.djl.crowd.util.exception.AccessForbiddenException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @desc 登录检查
 * @Author le
 * @Date 2021/11/10 9:17
 * @Version 1.0
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    /**
     * 方法之前执行
     *
     * @param request
     * @param response`
     * @param handler
     * @return true放行请求，反之亦然
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取session对象
        HttpSession session = request.getSession();
        // 取出session域中的用户信息
        Admin adminUser = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
        // 判断用户信息是否为空
        if (adminUser == null) {
            // 为空，抛出异常
            throw new AccessForbiddenException(CrowdConstant.ACCESS_FORBIDEN);
        }

        return true;
    }




}
